// pages/group-in/group-in.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    num: 0,
    groups: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this
    this.setData({
      num: parseInt(options.num)
    })

  },

  toGroup: function(e) {
    var that = this
    let index = e.currentTarget.dataset.index
    wx.navigateTo({
      url: '../one_group/one_group?_id=' + that.data.groups[index].group_id + "&name=" + that.data.groups[index].name + "&num=" + that.data.num,
    })
  },

  handleClick: function() {
    var that = this
    if(that.data.num == 1){
      // 允许从相机和相册扫码
      wx.scanCode({
        success(res) {
          console.log(res.result)
          wx.login({
            success(resout) {
              if (resout.code) {
                // 加入群组
                wx.request({
                  url: "https://www.ourspark.org/helloworld/join" ,
                  data: {
                    code: resout.code,
                    _id: res.result.split(",")[0],
                    name: res.result.split(",")[1]
                  },
                  method: "POST",
                  header: {
                    "Content-Type": "application/x-www-form-urlencoded"
                  },
                  success: function (res) {
                    console.log(res)
                    if (res.data.error_no == 2) {
                      wx.showModal({
                        title: "哎呀~",
                        content: res.data.error_msg,
                      })
                    } else if (res.data.error_no == 1) {
                      wx.login({
                        success(resout) {
                          if (resout.code) {
                            // 发起网络请求
                            wx.request({
                              url: 'https://www.ourspark.org/helloworld/groupIn',
                              data: {
                                code: resout.code,
                                num: that.data.num
                              },
                              method: "POST",
                              header: {
                                "Content-Type": "application/x-www-form-urlencoded"
                              },
                              success: function (res) {
                                console.log(res)
                                if (res.data.error_no == 2) {
                                  wx.showModal({
                                    title: "哎呀~",
                                    content: res.data.error_msg,
                                  })
                                } else if (res.data.error_no == 1) {
                                  if (res.data.groups) {
                                    let seg = ""
                                    if (that.data.num == 1) seg = "groups"
                                    else if (that.data.num == 2) seg = "C_groups"
                                    that.setData({
                                      groups: res.data.groups[seg]
                                    })
                                  } else {
                                    that.setData({
                                      groups: []
                                    })
                                  }

                                }
                              },
                              fail: function (res) {

                              }
                            })
                          } else {
                            console.log('登录失败！' + res.errMsg)
                          }
                        }
                      })

                    }
                  },
                  fail: function (res) {

                  }
                })
              } else {
                console.log('登录失败！' + res.errMsg)
              }
            }
          })
        }
      })

    } else if (that.data.num == 2){
      wx.navigateTo({
        url: '../new_group/new_group',
      })
    }
    
  },

  send: function(e) {
    var that = this
    let index = e.currentTarget.dataset.index
    wx.navigateTo({
      url: '../send/send?_id=' + that.data.groups[index].group_id,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this

    wx.login({
      success(resout) {
        if (resout.code) {
          // 发起网络请求
          wx.request({
            url: 'https://www.ourspark.org/helloworld/groupIn',
            data: {
              code: resout.code,
              num: that.data.num
            },
            method: "POST",
            header: {
              "Content-Type": "application/x-www-form-urlencoded"
            },
            success: function(res) {
              console.log(res)
              if (res.data.error_no == 2) {
                wx.showModal({
                  title: "哎呀~",
                  content: res.data.error_msg,
                })
              } else if (res.data.error_no == 1) {
                if (res.data.groups) {
                  let seg = ""
                  if (that.data.num == 1) seg = "groups"
                  else if (that.data.num == 2) seg = "C_groups"
                  that.setData({
                    groups: res.data.groups[seg]
                  })
                } else {
                  that.setData({
                    groups: []
                  })
                }

              }
            },
            fail: function(res) {

            }
          })
        } else {
          console.log('登录失败！' + res.errMsg)
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})